package com.naxiang.zhaohang.naxiangtimeandadress;

import android.app.Dialog;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import com.naxiang.zhaohang.pickerview.builder.TimePickerBuilder;
import com.naxiang.zhaohang.pickerview.listener.OnTimeSelectChangeListener;
import com.naxiang.zhaohang.pickerview.listener.OnTimeSelectListener;
import com.naxiang.zhaohang.pickerview.view.TimePickerView;
import com.uzmap.pkg.uzcore.UZWebView;
import com.uzmap.pkg.uzcore.uzmodule.UZModule;
import com.uzmap.pkg.uzcore.uzmodule.UZModuleContext;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;

/**
 * 作者: 赵航
 * 创建时间: 2018/5/30 9:30
 * 描述:
 */
public class TimeAdressShow extends UZModule {
    private TimePickerView  pvTime;
    private UZModuleContext mModuleContext;

    public TimeAdressShow(UZWebView webView) {
        super(webView);
    }

    //打开时间选择器
    public void jsmethod_openNaxiangDate(UZModuleContext moduleContext) {
        initTimePicker();
        if (pvTime != null) {
            pvTime.show();//弹出时间选择器，传递参数过去，回调的时候则可以绑定此view
        }
    }

    //返回时间选择
    public void jsmethod_openNaxiangDateSelect(UZModuleContext moduleContext) {
        mModuleContext = moduleContext;
    }

    /**
     * 时间转换
     *
     * @return
     */
    private String getTime(Date date) {//可根据需要自行截取数据显示
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private void initTimePicker() {
        pvTime = new TimePickerBuilder(getContext(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String time = getTime(date);

                System.out.println(time);
                JSONObject object = new JSONObject();
                try {
                    object.put("time", time);
                    mModuleContext.success(object, true);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {

                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isDialog(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
    }


}
